package org.glut.AppStoreProject.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.glut.AppStoreProject.entity.Download;
import org.glut.AppStoreProject.service.DownloadServiceImpl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author: gluglu2
 * @date: 2025/7/18 10:59
 */
@WebServlet(value = {"/download"})
public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        DownloadServiceImpl downloadService = new DownloadServiceImpl();
        Gson gson = null;

        if (req.getParameter("delete") == null) {
            if (req.getParameter("userId") == null) {
                return;
            } else {
                Download download = new Download();
                download.setUserId(Integer.valueOf(req.getParameter("userId")));
                if (req.getParameter("appId") != null) {
                    // json --> Object
//                gson = new Gson();
//                String reqJson = jsonFormRequest(req);
//                download = gson.fromJson(reqJson, Download.class);
                    download.setAppId(Integer.valueOf(req.getParameter("appId")));
                    System.out.println(download);
                    if (downloadService.insertDownload(download)) {
                        writer.write("下载成功");
                    } else {
                        writer.write("下载失败");
                    }
                } else {
                    // Object --> json
                    List<Download> downloadList = downloadService.getDownloadByUserId(download);
                    gson = new GsonBuilder()
                            .setDateFormat("yyyy-MM-dd HH:mm:ss")
                            .setPrettyPrinting()
                            .create();
                    writer.write(gson.toJson(downloadList));
                }
            }
        } else {
            Download download = new Download();
            download.setUserId(Integer.valueOf(req.getParameter("userId")));
            download.setAppId(Integer.valueOf(req.getParameter("appId")));
            if (downloadService.uninstallDownload(download)) {
                writer.write("卸载成功");
            } else {
                writer.write("卸载失败");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    /***
     * @description: 转换json格式
     * @param req
     * @return java.lang.String
     * @author: gluglu2
     * @date: 2025/7/18 11:07
     */
    private String jsonFormRequest(HttpServletRequest req) {
        String line = null;
        StringBuilder buff = new StringBuilder();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8));
            while ((line = br.readLine()) != null) {
                buff.append(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return buff.toString();
    }
}
